/**
 * 
 */
package com.rf.richfitwheel.admin.sys.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.rf.richfitwheel.admin.sys.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rf.richfitwheel.admin.sys.dao.OrgMapper;
import com.rf.richfitwheel.admin.sys.dao.SqlAdapterMapper;
import com.rf.richfitwheel.admin.sys.dao.TenantMapper;
import com.rf.richfitwheel.admin.sys.model.Org;
import com.rf.richfitwheel.admin.sys.model.Tenant;
import com.rf.richfitwheel.admin.sys.model.TenantExample;
import com.rf.richfitwheel.admin.sys.service.TenantService;
import com.rf.richfitwheel.common.contants.Constant;
import com.rf.richfitwheel.common.utils.PageUtils;
import org.springframework.util.StringUtils;
import com.rf.richfitwheel.common.utils.UuidUtil;

import net.sf.json.JSONObject;

/**
 * @author hushouquan
 *
 */
@Service("tenantService")
public class TenantServiceImpl implements TenantService {

	@Autowired
	private TenantMapper tenantMapper;
	
	@Autowired
	private OrgMapper orgMapper;
	
	@Autowired
   	private SqlAdapterMapper sqlAdapterMapper;//注入dao
	
	/**
	 * 分页查询
	 * @param params
	 * @return
	 */
	@Override
	public PageUtils queryPage(Map<String, Object> params) {
		PageUtils pageUtils=PageUtils.getPageInfo(params);
		Page<Tenant> p = PageHelper.startPage(pageUtils.getPage(), pageUtils.getLimit());
		Tenant tenant = (Tenant)JSONObject.toBean(JSONObject.fromObject(params.get("dataForm")),Tenant.class);
		TenantExample example = new TenantExample();
		TenantExample.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(tenant.getTenantCode())) {
			criteria.andTenantCodeLike("%" + tenant.getTenantCode() + "%");
		}
		if(!StringUtils.isEmpty(tenant.getTenantName())) {
			criteria.andTenantNameLike("%" + tenant.getTenantName() + "%");
		}
		example.setOrderByClause(" mod_time desc ");
		List<Tenant> list = tenantMapper.selectByExample(example);
		pageUtils =new PageUtils(p);
		return pageUtils;
	}

	/**
	 * 保存配置信息
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
    public void save(Tenant tenant, UserVO userVO) {
		Date nowDate = new Date();
		tenant.setId(UuidUtil.get32UUID());
		tenant.setCreateTime(nowDate);
		tenant.setModTime(nowDate);
		tenant.setCreater(userVO.getId());
		tenant.setModUser(userVO.getId());
		tenantMapper.insert(tenant);
		//创建租户时为该租户创建顶级组织机构
		Org org = new Org();
		org.setId(UuidUtil.get32UUID());
		org.setCreater(userVO.getId());
		org.setCreateTime(nowDate);
		org.setIsParent(Constant.COMMON_YES_OR_NO_YES);
		org.setOrdernum(0);
		org.setOrgCode(tenant.getTenantCode());
		org.setOrgLevel(1);
		org.setOrgName(tenant.getTenantName());
		org.setOrgRank(Constant.OrgRank.ORGRANK_COMPANY.getValue());
		org.setOrgStatus(Constant.COMMON_STATUS_NORMAL);
		org.setOrgType(Constant.OrgType.ORGRANK_PRACTICAL.getValue());
		org.setParentId("0");
		org.setShortName(tenant.getTenantName());
		org.setTenantId(tenant.getId());
		org.setVersion(0);
		orgMapper.insert(org);
	}

	/**
	 * 更新配置信息
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
    public void update(Tenant tenant, UserVO userVO) {
		Date nowDate = new Date();
		tenant.setModTime(nowDate);
		tenant.setModUser(userVO.getId());
		tenantMapper.updateByPrimaryKeySelective(tenant);
	}

	/**
	 * 删除配置信息
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
    public void deleteBatch(String[] ids) {
		for(String id:ids){
			tenantMapper.deleteByPrimaryKey(id);
		}
	}

	/**
	 * 主键获取
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@Override
	public Tenant selectById(String id) throws Exception {
		return tenantMapper.selectByPrimaryKey(id);
	}

	/**
	 * 根据tenantCode获取主键id，没有则返回null
	 * @param tenantCode
	 * @return
	 */
	@Override
	public String getTenantIdByCode(String tenantCode) {
		if(StringUtils.isEmpty(tenantCode)) {
			return null;
		}
		TenantExample example = new TenantExample();
		TenantExample.Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(tenantCode)) {
			criteria.andTenantCodeEqualTo(tenantCode);
		}
		List<Tenant> list = tenantMapper.selectByExample(example);
		if(!list.isEmpty()) {
			return list.get(0).getId();
		}
		return null;
	}

	/**
	 * 查询租户信息{key:'', value:''}
	 * @return
	 */
	@Override
	public List<Map<String, Object>> getTenantSelect() {
		return tenantMapper.getTenantSelect();
	}

}
